package com.game.layer;

import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;

import com.game.animation.AnimationObject;
import com.game.basic.BasicTouchableInterface;
import com.game.basic.BasicView;
import com.game.state.BasicState;
import com.game.utils.PhoneInfo;


/**
* @author oven:
* @version 创建时间：2012-11-8 下午3:06:00
* 
*/
public abstract class BasicLayer extends AnimationObject implements BasicTouchableInterface{
//	protected Paint paint = Tools.createNewPaint();
	protected BasicState state;
//	protected byte layerState=0;
	protected boolean show=false;//是否显示这个图层
	protected boolean coverDraw=false;//是否完全遮盖下一个图层
	protected boolean coverTouch=false;//是否禁止下个图层的点击事件
	protected boolean initIng=true;
	
	protected boolean isSoild=true;//标识图层范围是否实心的,还是只有一部分可以点击
	protected boolean dragable=false;//图层是否可以拖动
	protected boolean pressed=false;//标识点击图层
	
	protected float contentWidth;
	protected float contentHeight;
	
	protected float currentTransDisx;//当前拖动的距离
	protected float currentTransDisy;
	
	protected float holdScreenX;//第一次触摸的屏幕的位置
	protected float holdScreenY;
	protected boolean draging=false;//标识拖拉图层
	protected float holdx;//触摸住图层的位置
	protected float holdy;
	protected float transx;//移动的距离  小于0
	protected float transy;
	
	
	public BasicLayer(BasicState state){
		this.state=state;
		//初始化图层大小
		basicRegion.setWidth(PhoneInfo.screenWidth_def);
		basicRegion.setHeight(PhoneInfo.screenHeight_def);
		contentWidth=PhoneInfo.screenWidth_def;
		contentHeight=PhoneInfo.screenHeight_def;
		drawRegion=false;//图层默认不绘制区域颜色
//		initLayer();
	}
	
	
	/**
	 * 初始化这个图层
	 */
	public void initLayer(){
		//考虑线程
		Log.i("infos","开始初始化图层"+this.getClass());
		init();
		Log.i("infos","初始化图层完成"+this.getClass());
		initIng=false;
	}
	
	@Override
	public void draw(Canvas canvas) {
		//考虑线程同步
//		synchronized (this) {
		if(initIng){
			//把初始化放在这里
			initLayer();
		}else{
			//截取canvas大小为图层大小
			canvas.save();
			canvas.clipRect(getRegionRectF());
			//移动图层
			if(dragable){
				canvas.translate(getPostX()+transx,getPostY()+transy);
			}else{
				canvas.translate(getPostX(), getPostY());
			}
			//调用AnimationObject的draw方法
			super.draw(canvas);
			//绘制拖拉的线段
//			canvas.drawLine(holdx, holdy, holdx+currentTransDisx, holdy+currentTransDisy, paint);
			canvas.restore();
		}
//		}
	}
	
	/**
	 * 初始化方法
	 */
	public abstract void init();
	
	/**
	 * 绘制方法
	 */
	public abstract void paint(Canvas canvas);
	
	/**
	 * 注册当前为触摸中
	 */
	public void registerTouchEvent(){
		Log.i("infos", "[图层独占触摸]"+this.getClass());
		state.touchIngLayer=this;
		//表示为触摸按下状态
		pressed=true;
	}
	
	/**
	 * 标识离开
	 */
	public void releaseTouchEvent(){
		Log.i("infos", "[图层释放触摸]"+this.getClass());
		state.touchIngLayer=null;
		pressed=false;
		draging=false;
	}
	
	public boolean onTouchEvent(MotionEvent event){
		//初始化中,动画绘制中,不能触摸
		if(gradualchangIng||initIng){
			return false;
		}
		int action = event.getAction();
		//触摸的屏幕坐标
		float screenX = event.getX();
		float screenY = event.getY(); 
		
		//把坐标转化为相对图层的坐标
		float eventX=screenX;
		float eventY=screenY;
		eventX=eventX-getPostX();
		eventY=eventY-getPostY();
		
		if(dragable){
			eventX=eventX-transx;
			eventY=eventY-transy;
		}
		
		switch(action){
		case MotionEvent.ACTION_DOWN:{
			//判断是否触摸了图层区域
			if(!isTouchLayer(screenX,screenY)){
				return false;
			}
			
			currentTransDisx=0;
			currentTransDisx=0;
			
			if(isSoild){//全个layer都可点击
				Log.i("infos", "[图层接收down]"+this.getClass());
				//本图层成功响应按下事件,独占触摸
				registerTouchEvent();
				
				
				//标识图层上的精灵是否接收到点击信息
				boolean childClick = false;
				childClick = pointerPress(eventX,eventY);
				//更新触摸屏幕的位置
				holdScreenX=screenX;
				holdScreenY=screenY;
				//更新触摸图层的位置
				holdx=eventX;
				holdy=eventY;
				//精灵没接收到点击事件,可以拖动
				draging=!childClick;
				return true;
			}else{
				//标识图层上的精灵是否接收到点击信息
				boolean childClick = false;
				childClick = pointerPress(eventX,eventY);
				if(childClick){
					registerTouchEvent();
					//更新触摸屏幕的位置
					holdScreenX=screenX;
					holdScreenY=screenY;
					//更新触摸图层的位置
					holdx=eventX;
					holdy=eventY;
					//镂空的图层不允许拖动
					draging=false;
					return true;
				}
				return false;
			}
		}
		case MotionEvent.ACTION_MOVE:{
			if(!pressed){
				return false;
			}
			//计算当前触摸的位置于第一次触摸的位置的距离
			float distanceX = screenX-holdScreenX;
			float distanceY = screenY-holdScreenY;
			//计算前后的拖动距离的差值
			float distinceFixX = (distanceX-currentTransDisx); 
			float distinceFixY = (distanceY-currentTransDisy); 
			//更新为当前的拖动距离
			currentTransDisx=distanceX;
			currentTransDisy=distanceY;
			
			//拖动中,可拖动
			if(draging&&dragable&&(getWidth()<contentWidth||getHeight()<contentHeight)){
				float currentTransx=transx;
				float currentTransy=transy;
				transx=transx+distinceFixX;
				transy=transy+distinceFixY;
				//只能往左移动
				if(transx>0){
					transx=0;
				}
				//只能往上移动
				if(transy>0){
					transy=0;
				}
				//内容区域大于显示区域才可以移动
				if(getWidth()<contentWidth){
					//限制最大移动距离
					if(Math.abs(transx)>contentWidth-getWidth()){
						transx=getWidth()-contentWidth;
					}
				}
				if(getHeight()<contentHeight){
					if(Math.abs(transy)>contentHeight-getHeight()){
						transy=getHeight()-contentHeight;
					}
				}
				
				//因为图层内容是可以拖动的,所以按住图层的位置也会跟着改变
//				holdx=eventX;
//				holdy=eventY;
				holdx=holdx+(transx-currentTransx);
				holdy=holdy+(transy-currentTransy);
//				//移动像素>5才算移动,避免抖动
//				if(Math.abs(distanceX)>5||Math.abs(distanceY)>5){
//					if(Math.abs(distinceFixX)>5||Math.abs(distinceFixY)>5){
//						Log.i("infos", "[图层接收drag]"+this.getClass());
//						return pointerDrag(holdx, holdy, eventX, eventY);
//					}
//				}
			}

			
			if(Math.abs(distanceX)>5||Math.abs(distanceY)>5){
				Log.i("infos", "[图层接收drag]"+this.getClass());
				return pointerDrag(holdx, holdy, eventX, eventY);
			}
			
			//不满足拖动 move
			return pointerMove(eventX,eventY);
		}
		case MotionEvent.ACTION_UP:{
			if(!pressed){
				return false;
			}
			Log.i("infos", "[图层接收up]"+this.getClass());
			//释放触摸事件
			releaseTouchEvent();
//			holdx=transx;
//			holdy=transy;
			currentTransDisx=0;
			currentTransDisy=0;
			holdScreenX=0;
			holdScreenY=0;
			//判断提起
			return pointerRelaese(eventX,eventY);
		}
		default: return false;
		}
	}
	
	
	/**
	 * 把触摸的图层的坐标值转换为屏幕的坐标值
	 * @param x
	 * @return
	 */
	public float changeToScreenX(float x){
		//把坐标转化为相对图层的坐标
		float screenx = x + getPostX();
		if(dragable){
			screenx=screenx+transx;
		}
		return screenx;
	}
	
	/**
	 * 把触摸的图层的坐标值转换为屏幕的坐标值
	 * @param x
	 * @return
	 */
	public float changeToScreenY(float y){
		//把坐标转化为相对图层的坐标
		float screeny = y + getPostY();
		if(dragable){
			screeny=screeny+transx;
		}
		return screeny;
	}
	
	
    // 单击，触摸屏按下时立刻触发 
	public boolean onDown(MotionEvent e) {
		Log.i("infos", "[图层]点击::"+this.getClass());
		return pointerPress(e.getX(),e.getY());
	}

	// 抬起，手指离开触摸屏时触发(长按、滚动、滑动时，不会触发这个手势) 
	public boolean onSingleTapUp(MotionEvent e) {
		Log.i("infos", "[图层]点击抬起::"+this.getClass());
		return pointerRelaese(e.getX(),e.getY());
	}
	
	// 短按，触摸屏按下后片刻后抬起，会触发这个手势，如果迅速抬起则不会 
	public boolean onShowPress(MotionEvent e) {
		return false;
	}
	
	// 长按，触摸屏按下后既不抬起也不移动，过一段时间后触发 
	public boolean onLongPress(MotionEvent e) {
		return false;
	}
	
	// 滑动，触摸屏按下后快速移动并抬起，会先触发滚动手势，跟着触发一个滑动手势 
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		return false;
	}
	
	// 滚动，触摸屏按下后移动 
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
		Log.i("infos", "[图层]移动::"+this.getClass());
		return pointerMove(e2.getX(), e2.getY());
	}
	
	// 双击，手指在触摸屏上迅速点击第二下时触发
    public boolean onDoubleTap(MotionEvent e) { 

        return false; 
    } 

    // 双击的按下跟抬起各触发一次 
    public boolean onDoubleTapEvent(MotionEvent e) { 
        return false; 
    } 

    // 单击确认，即很快的按下并抬起，但并不连续点击第二下
    public boolean onSingleTapConfirmed(MotionEvent e) { 
        return false; 
    } 
	    
	public abstract boolean pointerPress(float x,float y);
	
	public abstract boolean pointerRelaese(float x,float y);
	
	public abstract boolean pointerMove(float x,float y);
	
	public abstract boolean pointerDrag(float sx,float sy, float ex,float ey);
	
	/**
	 * 显示图层
	 */
	public void show(){
		show=true;
	}
	
	/**
	 * 隐藏图层
	 */
	public void hide(){
		show=false;
	}
	
	/**
	 * 销毁图层
	 */
	public void destory(){
		state.removeLayer(this);
		System.gc();
	}
	
	/**
	 * 是否禁止其他层次绘制*/
	public boolean getCoverDraw(){
		if(!show){
			//未出现在屏幕中
			return false;
		}
		if(initIng){
			//未初始化完成,不绘制也不阻碍其他图层
			return false;
		}
		return coverDraw;
	}
	
	public boolean isTouchLayer(float x,float y){
		return BasicView.isTouchInRegion(x, y, getRegionWithAnimation());
	}

	public BasicState getState() {
		return state;
	}

	public void setState(BasicState state) {
		this.state = state;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public boolean isInitIng() {
		return initIng;
	}

	public void setInitIng(boolean initIng) {
		this.initIng = initIng;
	}

	public void setCoverDraw(boolean coverDraw) {
		this.coverDraw = coverDraw;
	}

	public void setCoverTouch(boolean coverTouch) {
		this.coverTouch = coverTouch;
	}


	public boolean isCoverTouch() {
		return coverTouch;
	}


	public float getTransx() {
		return transx;
	}


	public float getTransy() {
		return transy;
	}


	public void setTransx(float transx) {
		this.transx = transx;
	}


	public void setTransy(float transy) {
		this.transy = transy;
	}
}
